package com.xtm.exercise.base.A08Collection.A01list;

import com.xtm.exercise.base.B09copyBean.po.User;
import org.apache.commons.collections4.ListUtils;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 类描述：list的分批处理实现的几种方式
 *
 * @author lei
 * @version 1.0
 * @date 2023/3/8 15:34
 */
public class DemoSplitList {

    // list的分批处理实现的几种方式
    @Test
    public void test0(){
        // 创建模拟list
        List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10 , 11 ,12 ,13);
        // 按5个每组进行分割
        List<List<Integer>> subs = ListUtils.partition(intList, 5);
        // 对分批list的具体业务处理 process(list) - 打印代替
        System.out.println(subs);  // [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13]]
    }

    // 获取list中某个字段等于某值的集合
    @Test
    public void test01(){
        List<User> list = new ArrayList<>();
        User user1 = new User();
        user1.setAge(1);
        User user2 = new User();
        user2.setAge(2);

        list.add(user1);
        list.add(user2);

        List resultList = list.stream().filter(user -> Objects.equals(user.getAge(),"19")).collect(Collectors.toList());
        User o = (User) resultList.get(0);
        o.setName("wl");

        System.out.println(list.toString());
    }

    class UserInfo {
        public String name;
        public Integer age;
        public String classNo;

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", classNo='" + classNo + '\'' +
                    '}';
        }
    }

    public List<UserInfo> getUserList(int num) {
        List<UserInfo> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            UserInfo u = new UserInfo();
            u.age = i + 10;
            u.name = "a" + i;
            u.classNo = String.valueOf(i);
            list.add(u);
        }
        return list;
    }

    // 从List集合中删除指定元素
    @Test
    public void test02(){
        DemoSplitList listRemove = new DemoSplitList();
        //数据生成
        List<UserInfo> userList = listRemove.getUserList(5);
        System.out.println("原数据：" + Arrays.toString(userList.toArray()));
        //测试过滤掉 name = a2 或者 classNo = 4
        //方法一：Iterator删除
//        List<UserInfo> iteratorList = new ArrayList<>(userList);
//        Iterator<UserInfo> iterator = iteratorList.iterator();
//        while (iterator.hasNext()) {
//            UserInfo next = iterator.next();
//            if ("a2".equals(next.name) || "4".equals(next.classNo)) {
//                iterator.remove();
//            }
//        }
//        System.out.println("Iterator删除:" + Arrays.toString(iteratorList.toArray()));

        //方法二：removeIf删除
//        List<UserInfo> removeIfList = new ArrayList<>(userList);
//        removeIfList.removeIf(next -> "a2".equals(next.name) || "4".equals(next.classNo));
//        System.out.println("removeIf删除:" + Arrays.toString(removeIfList.toArray()));


        //方法三：使用filter
        List<UserInfo> filterList = new ArrayList<>(userList);
        filterList = filterList.stream().filter(e -> !"a2".equals(e.name) && !"4".equals(e.classNo)).collect(Collectors.toList());
        System.out.println("filter删除:" + Arrays.toString(filterList.toArray()));

        System.out.println("原数据：" + Arrays.toString(userList.toArray()));


    }
}
